package com.e2oto.seller.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * Created by ZhangLiQiang on 2017/8/5.
 */

public abstract class BasePresenter<T> {

    protected Reference<T> mViewRef;//View接口类型的弱引用

    public void attachView(T view) {

        mViewRef = new WeakReference<>(view);//建立关系
    }

    protected T getView() {

        return mViewRef.get();
    }

    protected Context getContext() {
        if (getView() instanceof Context) {
            return (Context) getView();
        }
        return null;
    }

    protected Activity getActivity() {
        if (getView() instanceof Activity) {
            return (Activity) getView();
        }
        return null;
    }

    public boolean isViewAttached() {

        return mViewRef != null && mViewRef.get() != null;
    }


    public void detachView() {

        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }

    }



}